package com.njitt.bysj.controller;

import com.njitt.bysj.base.BaseController;
import com.njitt.bysj.base.ResultInfo;
import com.njitt.bysj.bean.DeptBean;
import com.njitt.bysj.bean.EmpDeptBean;
import com.njitt.bysj.query.DeptQuery;
import com.njitt.bysj.srevice.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/3/19 11:38
 */
@Controller
@RequestMapping("dept")
public class DeptController extends BaseController {

    @Resource
    private DeptService deptService;

    @RequestMapping("toDeptListPage")
    public String toDeptListPage() {
        return "dept/dept";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> list(DeptQuery deptQuery) {
        Map<String, Object> map = deptService.selectDept(deptQuery);
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer id) {
        System.out.println(id);
        deptService.deleteDept(id);
        return success();
    }

    @RequestMapping("toAddOrUpdateDeptPage")
    public String toAddOrUpdateDeptPage(Integer deptId, HttpServletRequest request) {
        EmpDeptBean empDeptBean = deptService.selectEmpDept(deptId);
           request.setAttribute("empDept", empDeptBean);
        return "dept/add_update";
    }

    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addDept(DeptBean deptBean) {
        deptService.addDept(deptBean);
        return success("success");
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateDept(DeptBean deptBean) {
        deptService.updateDept(deptBean);
        return success("success");
    }

    @RequestMapping("deptSelected")
    @ResponseBody
    public List<Map<String, Object>> queryAllDeptToSelected() {
        return deptService.queryAllDeptToSelected();
    }

    @RequestMapping("empDept")
    @ResponseBody
    public Map<String, Object> empDept(DeptQuery deptQuery) {
        return deptService.empDept(deptQuery);
    }
}
